package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesSaleOrderRpcService;
import com.hunttown.mes.rpc.domain.MesSaleOrderDTO;
import com.hunttown.mes.rpc.domain.query.MesSaleOrderDTOQuery;
import com.hunttown.mes.service.MesSaleOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.MesSaleOrderConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:35:28
 */
@Service
public class MesSaleOrderRpcServiceImpl implements MesSaleOrderRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesSaleOrderRpcServiceImpl.class);

    private final MesSaleOrderService mesSaleOrderService;

    @Autowired
    public MesSaleOrderRpcServiceImpl(MesSaleOrderService mesSaleOrderService) {
        this.mesSaleOrderService = mesSaleOrderService;
    }

    @Override
    public MesSaleOrderDTO insert(MesSaleOrderDTO mesSaleOrderDTO) {
        try {
            return convert(mesSaleOrderService.insert(convert(mesSaleOrderDTO)));
        } catch (Exception e) {
            logger.error("MesSaleOrder create failed.", e);
            throw new RuntimeException("MesSaleOrder create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesSaleOrderDTO mesSaleOrderDTO) {
        try {
            return mesSaleOrderService.updateInfoById(convert(mesSaleOrderDTO));
        } catch (Exception e) {
            logger.error("MesSaleOrder update by id failed.", e);
            throw new RuntimeException("MesSaleOrder update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesSaleOrderDTOQuery mesSaleOrderDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleOrderDTOQuery.getW_id() != null && mesSaleOrderDTOQuery.getW_id() > 0) {
                query.put("w_id", mesSaleOrderDTOQuery.getW_id());
            }

            if (mesSaleOrderDTOQuery.getIds() != null && !mesSaleOrderDTOQuery.getIds().equals("")) {
                query.put("ids", mesSaleOrderDTOQuery.getIds());
            }

            if (mesSaleOrderDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleOrderDTOQuery.getStopFlag());
            }

            if (mesSaleOrderDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleOrderDTOQuery.getDeleteFlag());
            }

            return mesSaleOrderService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesSaleOrder update by query failed.", e);
            throw new RuntimeException("MesSaleOrder update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleOrderDTO selectById(Integer id) {
        try {
            return convert(mesSaleOrderService.selectById(id));
        } catch (Exception e) {
            logger.error("MesSaleOrder select by id failed.", e);
            throw new RuntimeException("MesSaleOrder select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleOrderDTO selectObjByQuery(MesSaleOrderDTOQuery mesSaleOrderDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getOrderNo())) {
                query.put("orderNo", mesSaleOrderDTOQuery.getOrderNo());
            }

            if (mesSaleOrderDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleOrderDTOQuery.getStopFlag());
            }

            if (mesSaleOrderDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleOrderDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getOrderBy())) {
                query.put("orderBy", mesSaleOrderDTOQuery.getOrderBy());
            }

            return convert(mesSaleOrderService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesSaleOrder select obj by query failed.", e);
            throw new RuntimeException("MesSaleOrder select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesSaleOrderDTO> getListForPage(MesSaleOrderDTOQuery mesSaleOrderDTOQuery) {
        Query query = Query.page(mesSaleOrderDTOQuery.getPn(), mesSaleOrderDTOQuery.getPageSize());

        if (mesSaleOrderDTOQuery.getId() != null && mesSaleOrderDTOQuery.getId() > 0) {
            query.put("id", mesSaleOrderDTOQuery.getId());
        }

        if (mesSaleOrderDTOQuery.getIds() != null && !mesSaleOrderDTOQuery.getIds().equals("")) {
            query.put("ids", mesSaleOrderDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getLikeName())) {
            query.put("likeName", mesSaleOrderDTOQuery.getLikeName());
        }

        if (mesSaleOrderDTOQuery.getCompanyId() != null && mesSaleOrderDTOQuery.getCompanyId() > 0) {
            query.put("companyId", mesSaleOrderDTOQuery.getCompanyId());
        }

        if (mesSaleOrderDTOQuery.getNomalOrder() != null && mesSaleOrderDTOQuery.getNomalOrder() == 1) {
            query.put("nomalOrder", mesSaleOrderDTOQuery.getNomalOrder());
        }

        if (mesSaleOrderDTOQuery.getStatus() != null) {
            query.put("status", mesSaleOrderDTOQuery.getStatus());
        }

        //区间日期
        if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getDaterange())) {
            String[] date = mesSaleOrderDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getShowDate())) {
            query.put("showDate", mesSaleOrderDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getStartDate())) {
            query.put("startDate", mesSaleOrderDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getEndDate())) {
            query.put("endDate", mesSaleOrderDTOQuery.getEndDate());
        }

        if (mesSaleOrderDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesSaleOrderDTOQuery.getStopFlag());
        }

        if (mesSaleOrderDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesSaleOrderDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getOrderBy())) {
            query.put("orderBy", mesSaleOrderDTOQuery.getOrderBy());
        }

        List<MesSaleOrderDTO> list = convert(mesSaleOrderService.selectByQuery(query));
        Page<MesSaleOrderDTO> page = Page.from(mesSaleOrderDTOQuery.getPn(), mesSaleOrderDTOQuery.getPageSize(), list);
        page.setTotalItems(mesSaleOrderService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesSaleOrderService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesSaleOrder delete failed.", e);
            throw new RuntimeException("MesSaleOrder delete failed, Please order your code.", e);
        }
    }

    @Override
    public Long countByQuery(MesSaleOrderDTOQuery mesSaleOrderDTOQuery) {
        try {
            Query query = Query.noPage();

            if (mesSaleOrderDTOQuery.getYearOnly() != null) {
                query.put("yearOnly", mesSaleOrderDTOQuery.getYearOnly());
            }

            if (mesSaleOrderDTOQuery.getYearMonth() != null) {
                query.put("yearMonth", mesSaleOrderDTOQuery.getYearMonth());
            }

            if (mesSaleOrderDTOQuery.getBeforeStatus() != null) {
                query.put("beforeStatus", mesSaleOrderDTOQuery.getBeforeStatus());
            }

            if (mesSaleOrderDTOQuery.getAfterStatus() != null) {
                query.put("afterStatus", mesSaleOrderDTOQuery.getAfterStatus());
            }

            if (mesSaleOrderDTOQuery.getStatusIds() != null) {
                query.put("statusIds", mesSaleOrderDTOQuery.getStatusIds());
            }

            query.put("stopFlag", 0);

            return mesSaleOrderService.countByQuery(query);
        } catch (Exception e) {
            logger.error("MesSaleOrder delete failed.", e);
            throw new RuntimeException("MesSaleOrder count failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> getMapDataByQuery(MesSaleOrderDTOQuery mesSaleOrderDTOQuery, String element) {
        try {
            Query query = Query.noPage();

            if (mesSaleOrderDTOQuery.getShowDate() != null) {
                query.put("showDate", mesSaleOrderDTOQuery.getShowDate());
            }

            if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getStartDate())) {
                query.put("startDate", mesSaleOrderDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getEndDate())) {
                query.put("endDate", mesSaleOrderDTOQuery.getEndDate());
            }

            if (mesSaleOrderDTOQuery.getYearMonth() != null) {
                query.put("yearMonth", mesSaleOrderDTOQuery.getYearMonth());
            }

            if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getStartYearMonth())) {
                query.put("startYearMonth", mesSaleOrderDTOQuery.getStartYearMonth());
            }

            if (StringUtils.isNotBlank(mesSaleOrderDTOQuery.getEndYearMonth())) {
                query.put("endYearMonth", mesSaleOrderDTOQuery.getEndYearMonth());
            }

            query.put("stopFlag", 0);
            query.put("deleteFlag", 0);

            return mesSaleOrderService.getMapDataByQuery(query, element);
        } catch (Exception e) {
            logger.error("MesSaleOrder select map data failed.", e);
            throw new RuntimeException("MesSaleOrder select map data failed, Please order your code.", e);
        }
    }
}